﻿<UserControl x:Class="Waf.BookLibrary.Library.Presentation.Views.BookListView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:c="clr-namespace:Waf.BookLibrary.Library.Presentation.Converters"
    xmlns:p="clr-namespace:Waf.BookLibrary.Library.Presentation.Properties"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:waf="http://waf.codeplex.com/schemas"
    xmlns:vm="clr-namespace:Waf.BookLibrary.Library.Applications.ViewModels;assembly=Waf.BookLibrary.Library.Applications"
    mc:Ignorable="d" d:DataContext="{d:DesignData Source=../DesignData/SampleBookListViewModel.xaml}"
    d:DesignWidth="550" d:DesignHeight="150"
    waf:ValidationHelper.IsEnabled="true" waf:ValidationHelper.IsValid="{Binding IsValid, Mode=OneWayToSource}">
    
    <DockPanel>
        <ToolBarTray DockPanel.Dock="Top" Margin="0,1,0,1">
            <ToolBar>
                <Button Command="{Binding AddNewCommand}">
                    <AccessText Text="{x:Static p:Resources.AddMenu}"/>
                </Button>
                <Button Command="{Binding RemoveCommand}">
                    <AccessText Text="{x:Static p:Resources.RemoveMenu}"/>
                </Button>
                <Separator/>
                <Label Content="{x:Static p:Resources.SearchMenu}" Target="{Binding ElementName=searchBox}" Padding="5,0,7,0" Margin="0" 
                       VerticalAlignment="Center"/>
                <TextBox x:Name="searchBox" Width="150" BorderBrush="LightGray" BorderThickness="1" 
                         Text="{Binding Path=FilterText, UpdateSourceTrigger=PropertyChanged}" TextChanged="FilterBoxTextChanged"/>
            </ToolBar>
        </ToolBarTray>

        <DataGrid x:Name="bookTable" ItemsSource="{Binding Books}" SelectedItem="{Binding SelectedBook, ValidatesOnNotifyDataErrors=False}" 
                  SelectionChanged="DataGridSelectionChanged" CanUserDeleteRows="False" BorderThickness="0">
            <DataGrid.InputBindings>
                <KeyBinding Command="{Binding RemoveCommand}" Key="Del"/>
            </DataGrid.InputBindings>
            
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Book.Title, UpdateSourceTrigger=PropertyChanged, ValidatesOnExceptions=True, NotifyOnValidationError=True}" 
                                    Header="{x:Static p:Resources.Title}" Width="5*" ElementStyle="{StaticResource TextCellElementStyle}"
                                    EditingElementStyle="{StaticResource TextCellEditingStyle}"/>
                <DataGridTextColumn Binding="{Binding Book.Author, UpdateSourceTrigger=PropertyChanged, ValidatesOnExceptions=True, NotifyOnValidationError=True}" 
                                    Header="{x:Static p:Resources.Author}" Width="3*" ElementStyle="{StaticResource TextCellElementStyle}"
                                    EditingElementStyle="{StaticResource TextCellEditingStyle}"/>
                <DataGridTextColumn Binding="{Binding Book.PublishDate, StringFormat=d, ValidatesOnExceptions=True, NotifyOnValidationError=True}" 
                                    Header="{x:Static p:Resources.PublishDate}" Width="90" ElementStyle="{StaticResource TextCellElementStyle}"
                                    EditingElementStyle="{StaticResource TextCellEditingStyle}"/>
                <DataGridTemplateColumn Header="{x:Static p:Resources.LendTo}" IsReadOnly="True" SortMemberPath="Book.LendTo.Firstname" Width="3*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="Auto"/>
                                </Grid.ColumnDefinitions>
                                <TextBlock Padding="4,2,4,3">
                                    <TextBlock.Text>
                                        <MultiBinding StringFormat="{}{0} {1}">
                                            <Binding Path="Book.LendTo.Firstname" ValidatesOnNotifyDataErrors="False"/>
                                            <Binding Path="Book.LendTo.Lastname" ValidatesOnNotifyDataErrors="False"/>
                                        </MultiBinding>
                                    </TextBlock.Text>
                                </TextBlock>
                                <TextBlock Grid.Column="1" Padding="4,2,4,3">
                                    <Hyperlink Command="{Binding LendToCommand}" CommandParameter="{Binding Book}">
                                        <Run Text="{x:Static p:Resources.MoreButton}"/>
                                    </Hyperlink>
                                </TextBlock>
                            </Grid>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </DockPanel>
</UserControl>
